package away3d.core.managers ;
/**
 * Author: David Lenaerts
 */


import away3d.materials.utils.CubeMap ;
import flash.display3D.Context3D ;
import flash.display3D.Context3DTextureFormat ;
import flash.display3D.textures.CubeTexture ;



class CubeTexture3DProxy
{
    
    
    private var _cubeMap    : CubeMap ;
    private var _textures   : Vector<CubeTexture> ;
    private var _dirty      : Vector<Bool> ;
    private var _maxIndex   : Int ;
    
    
    
    public function CubeTexture3DProxy()
    {
        
        _maxIndex   = -1 ;
        _textures   = new Vector<CubeTexture>( 8 ) ;
        _dirty      = new Vector<Bool>( 8 ) ;
        
    }
    
    
    private function get_cubeMap() : CubeMap
    {
        
        return _cubeMap ;
        
    }
    
    
    private function set_cubeMap( value : CubeMap )
    {
        
        if( value == _cubeMap ) return ;
        
        if( _cubeMap ) 
        {
            
            if( value.size != _cubeMap.size )
            {
                
                invalidateSize() ;
            
            }
            else
            {
                
                invalidateContent() ;
            }
            
        }
        
        _cubeMap = value ;
        
    }
    
    
    public function invalidateContent()
    {
        
        for( i in 0... _maxIndex )
        {
            
            _dirty[ i ] = true ;
            
        }
        
    }
    
    
    private function invalidateSize()
    {
        
        var tex : CubeTexture ;
        
        for ( i in 0..._maxIndex )
        {
            
            tex = _textures[ i ] ;
            
            if( tex )
            {
                
                tex.dispose() ;
                _textures[ i ]  = null ;
                _dirty[ i ]     = false ;
                
            }
            
        }
        
    }
    
    
    public function dispose( deep : Bool )
    {
        
        if( deep )
        {
            
            if( _cubeMap ) _cubeMap.dispose() ;
            
            _cubeMap = null ;
            
        }
        
        for( i in 0..._maxIndex )
        {
            
            if( _textures[ i ] ) _textures[ i ].dispose() ;
            
        }
        
    }
    
    
    public var TextureForContext( get_TextureForContext, null ) : CubeTexture ;
    
    
    private function get_TextureForContext( context : Context3D, contextIndex : int ) : CubeTexture
    {
        
        if( contextIndex > _maxIndex ) _maxIndex = contextIndex ;
        
        var tex : CubeTexture = _textures[contextIndex] ;
        
        if( !tex || _dirty[ contextIndex ] )
        {
            
            if( !tex ) _textures[ contextIndex ] = tex = context.createCubeTexture( _cubeMap.size, Context3DTextureFormat.BGRA, false ) ;
            
            _cubeMap.upload( _textures[ contextIndex ] ) ;
            _dirty[ contextIndex ] = false ;
            
        }
        
        return tex ;
        
    }
    
}
